#ifndef __NEW_TERRAIN_CONTENTS_GENERATOR_BASE_H__
#define __NEW_TERRAIN_CONTENTS_GENERATOR_BASE_H__

#include "Terrain/ITerrainContentsGenerator.h"
#include "Engine/Vector2.hpp"
#include <memory>
#include <vector>
#include "Terrain/TerrainBrush.h"

using namespace PocketSurvival::Engine;

namespace PocketSurvival
{
    class SubsystemGameInfo;
    class SubsystemTerrain;
    
    class NewTerrainContentsGenerator23 : public ITerrainContentsGenerator
    {
    public:
        static bool brushIsCreated;
        static std::vector<TerrainBrush> coalBrushes;
        static std::vector<TerrainBrush> ironBrushes;
        static std::vector<TerrainBrush> copperBrushes;
        static std::vector<TerrainBrush> saltpeterBrushes;
        static std::vector<TerrainBrush> sulphurBrushes;
        static std::vector<TerrainBrush> diamondBrushes;
        static std::vector<TerrainBrush> germaniumBrushes;
        static std::vector<TerrainBrush> dirtPocketBrushes;
        static std::vector<TerrainBrush> gravelPocketBrushes;
        static std::vector<TerrainBrush> limestonePocketBrushes;
        static std::vector<TerrainBrush> sandPocketBrushes;
        static std::vector<TerrainBrush> basaltPocketBrushes;
        static std::vector<TerrainBrush> granitePocketBrushes;
        static std::vector<TerrainBrush> clayPocketBrushes;
        static std::vector<TerrainBrush> waterPocketBrushes;
        static std::vector<TerrainBrush> magmaPocketBrushes;
        static std::vector<std::vector<TerrainBrush>> caveBrushesByType;

        static void CreateBrushes();

    private:
        int32_t m_seed;
        Vector2 m_islandSize;
        Vector2 m_oceanCorner;
        Vector2 m_temperatureOffset;
        Vector2 m_humidityOffset;
        Vector2 m_mountainsOffset;
        Vector2 m_riversOffset;

        float TGBiomeScaling;
        float TGShoreFluctuations;
        float TGShoreFluctuationsScaling;
        float TGOceanSlope;
        float TGOceanSlopeVariation;
        float TGIslandsFrequency;
        float TGDensityBias;
        float TGHeightBias;
        float TGHillsPercentage;
        float TGHillsStrength;
        int32_t TGHillsOctaves;
        float TGHillsFrequency;
        float TGHillsPersistence;
        float TGMountainsStrength;
        float TGMountainRangeFreq;
        float TGMountainsPercentage;
        float TGMountainsDetailFreq;
        int32_t TGMountainsDetailOctaves;
        float TGMountainsDetailPersistence;
        float TGRiversStrength;
        float TGTurbulenceStrength;
        float TGTurbulenceFreq;
        int32_t TGTurbulenceOctaves;
        float TGTurbulencePersistence;
        float TGMinTurbulence;
        float TGTurbulenceZero;
        float TGSurfaceMultiplier;
        bool TGWater;
        bool TGExtras;
        bool TGCavesAndPockets;

        SubsystemGameInfo *subsystemGameInfo;
        SubsystemTerrain *subsystemTerrain;

    public:
        NewTerrainContentsGenerator23();
        ~NewTerrainContentsGenerator23();

    public:
        virtual uint32_t getOceanLevel() override;

        virtual Vector3 findCoarseSpawnPosition() override;

        // 计算点位到海边的距离
        virtual float calculateOceanShoreDistance(float x, float z) override;

        virtual float calculateHeight(float x, float z) override;

        virtual int32_t calculateTemperature(float x, float z) override;

        virtual int32_t calculateHumidity(float x, float z) override;

        virtual float calculateMountainRangeFactor(float x, float z) override;

        float calculateOceanShoreX(float z);
        float calculateOceanShoreZ(float x);

        virtual void generateChunkContentsPass1(TerrainChunk *chunk) override;

        virtual void generateChunkContentsPass2(TerrainChunk *chunk) override;

        virtual void generateChunkContentsPass3(TerrainChunk *chunk) override;

        virtual void generateChunkContentsPass4(TerrainChunk *chunk) override;


        bool canSupportSnow(int32_t value);
        bool bottomSuckerIsSupport(int32_t value, int32_t face);

        // 生成曲面，温度和湿度的计算
        void generateSurfaceParameters(TerrainChunk *chunk);
        // 生成地形
        void generateTerrain(TerrainChunk *chunk);
        // 生成洞穴
        void generateCaves(TerrainChunk *chunk);
        // 生成地形杂质
        void generatePockets(TerrainChunk *chunk);
        // 生成矿物
        void generateMinerals(TerrainChunk *chunk);
        // 生成地面
        void generateSurface(TerrainChunk *chunk);
        // 流体向下延申
        void propagateFluidsDownwards(TerrainChunk *chunk);
        // 生成草地和植物
        void generateGrassAndPlants(TerrainChunk *chunk);
        // 生成倒下的树木
        void generateLogs(TerrainChunk *chunk);
        // 生成树
        void generateTrees(TerrainChunk *chunk);
        // 生成仙人掌
        void generateCacti(TerrainChunk *chunk);
        // 生成南瓜
        void generatePumpkins(TerrainChunk *chunk);
        // 生成海带
		void generateKelp(TerrainChunk *chunk);
        // 生成海草
		void generateSeagrass(TerrainChunk *chunk);
        // 生成海星和海胆
		void generateBottomSuckers(TerrainChunk *chunk);
        // 生成陷阱
        void generateTraps(TerrainChunk *chunk);
        // 生成藤蔓
        void generateIvy(TerrainChunk *chunk);
        // 生成坟墓
        void generateGraves(TerrainChunk *chunk);
        // 生成石碑
        void generateCairns(TerrainChunk *chunk);
        // 生成雪和冰
        void generateSnowAndIce(TerrainChunk *chunk);
        // 生成基岩和空气
        void generateBedrockAndAir(TerrainChunk *chunk);
        // 更新顶部流体
        void updateFluidIsTop(TerrainChunk *chunk);

    };

}

#endif